class Player extends egret.DisplayObjectContainer implements IPool {
	public constructor(vo: PlayerVO) {
		super();
		Player.instance = this;
		this.touchEnabled = false;
		this.touchChildren = false;
		this.vo = vo;
		this.init();
		//创建一个计时器对象
	}

	private static instance: Player;
	public static getInstance(): Player {
		return this.instance;
	}
	/**玩家球的大小 */
	protected bg_diameter: number;
	/**刀的基类 */
	public bg: BaseBitmap;
	/**玩家球显示的名字 */
	protected lbl_name: BaseTextField;
	/**刀的数组 */
	public knifes: BaseBitmap[];
	private p1: egret.Point = new egret.Point();
	private p2: egret.Point = new egret.Point();
	/**显示容器 */
	protected con: egret.DisplayObjectContainer;
	protected frame: number = 0;
	protected ui: number = 0;
	public vo: PlayerVO;


	protected init() {
		this.initData();
		if (this.vo != null) {
			this.initView();
		}
	}

	protected initData() {
		//初始化让刀等于0
		this.knifes = [];
		this.knife_index = 0;
	}

	public updateVO(vo: PlayerVO) {
		this.vo = vo;
		if (this.bg == null) {
			this.initView();
		}
		else {
			this.bg.texture = RES.getRes("yuan_" + this.vo.bg_id);
			this.updateView();
			this.updateKnifes();
		}
	}

	protected initView() {
		//实例化一个新的con
		this.con = new egret.DisplayObjectContainer();
		//添加场景
		this.addChild(this.con);
		if (this.bg == null) {
			this.state = PLAYERSTATE.CRUISE;
		}
		this.bg = ObjectPool.getObject("BaseBitmap") as BaseBitmap;
		this.bg.texture = RES.getRes("yuan_" + this.vo.bg_id);
		this.addChild(this.bg);
		this.lbl_name = ObjectPool.getObject("BaseTextField") as BaseTextField;
		this.lbl_name.fontFamily = "SimHei";
		this.lbl_name.textAlign = "center";
		this.lbl_name.text = this.vo.name;
		this.lbl_name.anchorOffsetX = this.lbl_name.width / 2;
		this.lbl_name.anchorOffsetY = this.lbl_name.height / 2;
		this.addChild(this.lbl_name);
		this.updateView();
		this.updateKnifes();
	}

	private _state: number;
	public get state(): number {
		return this._state;
	}
	public set state(v: number) {
		if (this._state == v) {
			return;
		}
		this.setState(v);
	}

	protected setState(v: number) {
		this._state = v;
		switch (v) {
			case PLAYERSTATE.CRUISE:
				this.wait();
				break;
			case PLAYERSTATE.MOVE:
				this.move();
				break;
			case PLAYERSTATE.DEFENCE:
				this.defence();
				break;
			case PLAYERSTATE.DEAD:
				break;
			case PLAYERSTATE.ATK:
				break;
		}
	}
	//初始化位置随机，只执行一次
	public an: boolean = true;
	protected wait() {
		if (this.an == true) {
			this.x = Math.random() * DesignConst.mapwidth;
			this.y = Math.random() * DesignConst.mapheight;
			this.an = false;
		}
	}
	protected move() {
	}
	protected defence() {
		this.vo.direction = null;//可视效果：首尾相连时，刀间距变大
		this.updateKnifes();
	}
	/**边界判断 */
	protected checkBound(): boolean {
		return false;
	}
	/**转向
	 * @param a 当b为null时 角度直接改变成a，否则a-b随机一个范围
	 * @param b 当b为null时 角度直接改变成a，否则a-b随机一个范围
	 */
	/** AI转向 */
	public changeDirection(a: number = 0, b: number = 360) {
		if (b == null) {
			this.vo.direction = a;
		}
		else {
			this.vo.direction = GameUtil.between(a, b, 0);
		}
	}

	/**玩家每帧更新坐标 */
	protected updateCrood() {
		let hu = this.vo.direction * Math.PI / 180;
		this.x += this.vo.move_speed * Math.sin(hu);
		this.y -= this.vo.move_speed * Math.cos(hu);
	}
	/**1秒后更新发射状态 */
	private timerComFunc() {

		this.state = PLAYERSTATE.CRUISE;
		//实例化一个新的con
		this.con = new egret.DisplayObjectContainer();
		//添加场景
		this.addChild(this.con);
		this.knife_index = 0;
		this.updateKnifes();
		this.ss = true;
	}
	/**0.3秒后调动子弹发射脚本 */
	private time() {
		this.atkk(this.jiao);
		return true;
	}
	/**获取的是移动方向 */
	public jiao: number;
	/**判断现在玩家的状态 */
	private ss: boolean = true;


	/** 每一帧执行 */
	public update() {
		/**控制玩家和敌人刀的每一帧选择 */
		if (this.state == PLAYERSTATE.ATK&&this.knifes.length > 0) {
			//如果是玩家移动则发射飞刀
			if (this.ss == true) {
				if (this.vo.direction != null) {
					this.jiao = this.vo.direction;
				}
				else {
					this.jiao = 0;
				}
			}
			if (this.ss == true && this.vo.name == "玩家") {
				this.con.rotation = 0;
				this.ss = false;
				if (this.knifes.length > 0) {
					egret.setTimeout(() => { this.time() }, this, 550);

				}
				egret.setTimeout(() => { this.timerComFunc() }, this, 1100);




			}
		}
		/**攻击时改变状态 */
		if (this.ss == false) {
			this.state = PLAYERSTATE.ATK;
		}
		if (this.state != PLAYERSTATE.ATK && this.state != PLAYERSTATE.DEFENCE) {
			this.con.rotation += this.vo.turn_speed;
		}
		if (this.vo.direction != null && this.vo.move_speed != null) {
			this.updateCrood();
		}
		this.checkEatKnife();
		this.frame++;
		if (this.state == PLAYERSTATE.DEFENCE && this.frame % 40 == 0) {	//如果this.frame>40且为40的倍数
			this.addCircle();
		}
		if (this.frame % 4 == 0) {	//如果this.frame>40且为40的倍数
			//刀的数量++
			this.knife_index++;
			//如果刀的数量大于等于刀数组的总长
			if (this.knife_index >= this.knifes.length) {
				//刀的数量归零
				this.knife_index = 0;
			}
			//传刀的数量进方法
			this.updateKnifeInArr(this.knife_index);
		}
		/**每帧调用生成刀的方法 */
		if (this.ss == false && this.n > this.b || this.n > this.b) {
			this.updateKnifes();
		}
		// else if (this.n > this.b) {
		// 	this.updateKnifes();
		// }

		/**每帧判断刀大于5时，玩家体积变化 */
		this.scale();
	}
	/**承接刀碰撞后返回的数量 */
	public back_knifes_num: number = 9;
	/**通过tween动画把刀发射出去 */
	public atkk(rotation) {
		let hu = rotation * Math.PI / 180;
		let a = 400 * Math.sin(hu);
		let b = 400 * Math.cos(hu);
		let x = this.con.x + a;
		let y = this.con.y - b;
		//let p = this.CheckBoun(x, y, 3);
		let p = this.CheckBound(x, y);//控制tween动画运行时移动方向及距离
		let t = this.knifes.length - 1;//声明t是为了在攻击时首先获取刀数组的长度，防止在其他状态时刀的损失
		this.back_knifes_num = this.knifes.length;
		for (let i = t; i >= 0; i--) {
			egret.Tween.get(this.knifes[i]).to({ x: p.x, y: p.y }, 500).call(() => {//this.knifes[i]：被移动物体，p.x, p.y：移动的目标点，600：移动到目标点需要的时间
				for (let j = this.knifes.length - 1; this.knifes.length > 0;) {//刀的tween动画在其他状态发生碰撞时会被删除，所以再删除时需要判断刀tween动画还有多少
					egret.Tween.removeTweens(this.knifes[this.knifes.length - 1]);
					this.removeKnife(this.knifes.length - 1);//删除数组里面的刀
				}
				/**运动结束删除物体 */
				if (this.knifes.length == 0) {
					this.state = PLAYERSTATE.CRUISE;
					this.removeChild(this.con);
				}
			}, this);

		}
	}

	/**通过tween动画把刀发射出去 *///暂时没用
	public atk(rotation) {
		// let hu = rotation * Math.PI / 180;
		// let a = 200 * Math.sin(hu);
		// let b = 200 * Math.cos(hu);
		// let x = this.con.x + a;
		// let y = this.con.y - b;
		// //	let p = this.CheckBoun(x, y, 3);
		// let p = this.CheckBound(x, y);//控制tween动画运行时移动方向及距离
		// egret.Tween.get(this.con).to({ x: p.x, y: p.y }, 600).call(() => {
		// 	egret.Tween.removeTweens(this.con);
		// 	//运动结束删除物体
		// 	this.state = PLAYERSTATE.CRUISE;
		// 	this.removeChild(this.con);
		// 	for (let i = 3; this.knifes.length > 0; i--) {
		// 		this.removeKnife(i);
		// 	}
		// 	console.log(this.knifes.length);
		// }, this);

	}
	public CheckBound(x, y): egret.Point {
		// if (x + this.x > DesignConst.mapwidth) {
		// 	x = DesignConst.mapwidth - this.x - 100;
		// }
		// if (y + this.y > DesignConst.mapheight) {
		// 	y = DesignConst.mapheight - this.y - 100;
		// }
		// if (x + this.x < 0) {
		// 	x = this.x + (x - (this.x + x));
		// }
		// if (y + this.y < 0) {
		// 	y = this.y + (y - (this.y + y));
		// }
		return new egret.Point(x, y);
	}

	protected addCircle() {
		let bmp = ObjectPool.getObject("BaseBitmap") as BaseBitmap;
		let r = Math.floor(Math.random() * 5 + 1);
		//bmp.texture = RES.getRes("yuan_" + r);
		this.addChildAt(bmp, 0);
		bmp.width = this.bg_diameter;
		bmp.height = this.bg_diameter;
		bmp.anchorOffsetX = this.vo.circleRadius;
		bmp.anchorOffsetY = this.vo.circleRadius;
		let s = 2.4;
		egret.Tween.get(bmp).to({ scaleX: s, scaleY: s, alpha: 0 }, 1200).call((bmp) => {
			if (bmp != null && bmp.parent != null) {
				bmp.parent.removeChild(bmp);
			}
		}, this, [bmp]);
	}

	//没有被调用
	public addKnifeNum(n: number) {
		// this.vo.knife_num += n;
		// this.updateKnifes();
	}

	/**碰撞后删除飞刀*/
	public removeKnife(i: number) {
		let knife = this.knifes[i];
		if (knife != null) {
			this.vo.knife_num--;
			this.knifes.splice(i, 1);
			if (knife.parent != null) {
				knife.parent.removeChild(knife);
			}
		}
	}

	/**获取到捡起飞刀数量并调动生成身上飞刀方法 */
	public checkEatKnife() {
		let n = Maplogic.getInstance().dealEatKnifes(this.x, this.y, this.vo.radius);
		//判断是否接触到飞刀，玩家身上飞刀数量是否超过5把
		if (n > 0)
			if (n > 0 && this.vo.knife_num <= 8) {
				this.vo.knife_num += n;
				this.updateKnifes();
			}
	}

	protected updateView() {
		this.updateBg();
		this.updateLblName();
	}
	/**碰撞后刀的数量 */
	public b: number;
	/**获取刀实时的数量 */
	public n: number;
	/**删除地上飞刀后在玩家身上生成新的飞刀 */
	protected updateKnifes() {
		//let n= Maplogic.getInstance().dealEatKnifes(this.x, this.y, this.vo.radius);
		this.b = this.back_knifes_num;
		//console.log(this.b + "身上刀数量");
		this.n = this.vo.knife_num;
		if (this.n > this.b && this.ss == false) {
			this.b = this.n - this.b + this.b - 1;
		}
		else if (this.n > this.b) {
			this.b = this.n - this.b + this.b;
		}
		//判断玩家飞刀数量是否大于5，若大于则N等于5
		if (this.b >= DesignConst.player_knife_max) {
			this.b = DesignConst.player_knife_max;
		}

		if (this.state == PLAYERSTATE.CRUISE || this.state == PLAYERSTATE.WAIT) {
			let knife_height = 115;
			this.vo.radius = knife_height * this.n / 2 / Math.PI / 2;
		}
		// else if (this.b >= 5) {
		// 	this.vo.radius = 115 * this.n / 2 / Math.PI;
		// }
		for (let i = 0; i < this.b; i++) {
			let v = this.vo.skinvo;
			let knife = this.knifes[i];
			if (knife == null) {
				knife = ObjectPool.getObject("BaseBitmap") as BaseBitmap;
				knife.texture = RES.getRes("dao_" + 3);
				knife.scaleX = knife.scaleY = DesignConst.knifescale;
				knife.anchorOffsetX = knife.width / 2;
				knife.state = KNIFESTATE.FLYING;
				if (this.state == PLAYERSTATE.DEFENCE) {
					knife.anchorOffsetY = -this.vo.circleRadius / 2 + knife.height;
				}
				else {
					knife.anchorOffsetY = -this.vo.radius + knife.height;
				}
				if (this.ss == false) {//按下攻击后更新刀的数量
					this.vo.knife_num++;
				}
				this.knifes.push(knife);
			}
			this.con.addChild(knife);
		}
		this.back_knifes_num = 0;
	}

	protected knife_index: number;
	/**刀状态调整 */
	protected updateKnifeInArr(i: number) {
		let knife = this.knifes[i];
		if (knife != null) {
			//声明一个变量n继承刀这个变量
			let n = this.vo.knife_num;
			//如果n大于等于刀的总数
			if (n >= DesignConst.player_knife_max) {
				//n=刀的总数
				n = DesignConst.player_knife_max;
			}

			if (this.state == PLAYERSTATE.CRUISE || this.state == PLAYERSTATE.WAIT) {
				let knife_height = 180;
				if (this.vo.knife_num < 5) {
					this.vo.radius = knife_height * 4 / 2 / Math.PI;
				}
				else {
					this.vo.radius = 120
					//this.vo.radius = knife_height * n / 2 / Math.PI;
				}
			}
			/**刀状态调整 */
			if (this.state == PLAYERSTATE.DEFENCE) {
				knife.anchorOffsetY = knife.height / 2;
				// if (this.vo.knife_num == 1) {//防御时，一把刀时，刀到玩家的距离
				// 	knife.anchorOffsetX =  (this.vo.radius + knife.width / 2) / DesignConst.knifescale;
				// }
				// else if (this.vo.knife_num == 2) {//防御时，两把刀时，刀到玩家的距离
				// 	knife.anchorOffsetX =  (this.vo.radius + knife.width / 2) / DesignConst.knifescale;
				// }
				// else if (this.vo.knife_num > 5) {//防御时，大于五把刀时，刀到玩家的距离
				// 	knife.anchorOffsetX = ((180 * 4 / 2 / Math.PI) + knife.width / 2) / DesignConst.knifescale;
				// }
				// else {
				knife.anchorOffsetX = (this.vo.radius + knife.width / 2) / DesignConst.knifescale;
				//	}
				if (this.vo.knife_num <= 5) {//防御时，刀小于5时，刀到刀的距离
					knife.rotation = 180 * i / 6;
				}
				else {
					knife.rotation = 180 * i / n;
				}
				knife.scaleX = 1;
				/**刀容器的角度跟随鼠标角度 */
				this.con.rotation = this.vo.Rightdirection;
			}
			if (this.state == PLAYERSTATE.CRUISE || this.state == PLAYERSTATE.WAIT) {
				knife.anchorOffsetX = -this.vo.circleRadius / 2 - knife.height / 2;//刀离玩家的距离
				knife.anchorOffsetY = knife.width / 2;
				if (this.vo.knife_num == 2 || this.vo.knife_num == 3) {//两把刀或三把刀时，刀与刀之间的间距

					knife.rotation = 180 * i / 4;
				}
				else {
					knife.rotation = 180 * i / n;
				}
				knife.scaleX = -1;	//镜像反转			
			}
			if (this.state == PLAYERSTATE.ATK && knife.state == KNIFESTATE.FLYING) {
				knife.anchorOffsetX = knife.width / 2;
				knife.anchorOffsetY = knife.height / 2;
				knife.rotation = this.jiao - 180;
				//左右移动
				if (this.jiao > 45 && this.jiao < 135) {
					// knife.x = this.vo.radius;
					// knife.y = -75 + 25 * i;
					//刀排列方式，刀与刀相距20
					if (i % 2 == 0) {
						knife.y = -10 * i;
					}
					else {
						knife.y = 10 * (i + 1);
					}
					//刀排列方式，刀成三角排列
					for (let j = 0; j <= i; j++) {
						if (j % 2 == 0) {
							if (j == 0) {
								knife.x = 107.5;
							}
							else if (j == 2) {
								knife.x = 97.5;
							}
							else {
								knife.x = -(16.25 * j - 130);
							}
						}
						else {
							if (j == 1) {
								knife.x = 97.5;
							}
							else {
								knife.x = -(16.25 * (j + 1) - 130);
							}
						}
					}
				}
				if (this.jiao < -45 || this.jiao > 225) {
					// knife.x = -this.vo.radius;
					// knife.y = -75 + 25 * i;
					//刀排列方式，刀与刀相距20
					if (i % 2 == 0) {
						knife.y = -10 * i;
					}
					else {
						knife.y = 10 * (i + 1);
					}
					//刀排列方式，刀成三角排列
					for (let j = 0; j <= i; j++) {
						if (j % 2 == 0) {
							if (j == 0) {
								knife.x = -107.5;
							}
							else if (j == 2) {
								knife.x = -97.5;
							}
							else {
								knife.x = 16.25 * j - 130;
							}
						}
						else {
							if (j == 1) {
								knife.x = -97.5;
							}
							else {
								knife.x = 16.25 * (j + 1) - 130;
							}
						}
					}
				}
				//上下移动
				if (this.jiao > -45 && this.jiao < 45) {
					//knife.x = -80 + 20 * i;
					//knife.y = -this.vo.radius;
					//刀排列方式，刀与刀相距20
					if (i % 2 == 0) {
						knife.x = -10 * i;
					}
					else {
						knife.x = 10 * (i + 1);
					}
					//刀排列方式，刀成三角排列
					for (let j = 0; j <= i; j++) {
						if (j % 2 == 0) {
							if (j == 0) {
								knife.y = -107.5;
							}
							else if (j == 2) {
								knife.y = -97.5;
							}
							else {
								knife.y = 16.25 * j - 130;
							}
						}
						else {
							if (j == 1) {
								knife.y = -97.5;
							}
							else {
								knife.y = 16.25 * (j + 1) - 130;
							}
						}
					}
					// let num = 0;
					// for (let j = 0; j < i; j++) {
					// 	if (j < 3) {
					// 		num = num - 32.5;
					// 		knife.y = num;
					// 	}
					// 	else if (j == 3) {
					// 		num = num - 10;
					// 		knife.y = num;
					// 	}
					// 	else if (j == 4) {
					// 		num = num + 10;
					// 		knife.y = num;
					// 	}
					// 	else if (j > 4) {
					// 		num = num + 32.5;
					// 		knife.y = num;
					// 	}
					// }
				}
				if (this.jiao < 225 && this.jiao > 135) {
					// knife.x = -75 + 25 * i;
					// knife.y = this.vo.radius
					//刀排列方式，刀与刀相距20
					if (i % 2 == 0) {
						knife.x = -10 * i;
					}
					else {
						knife.x = 10 * (i + 1);
					}
					//刀排列方式，刀成三角排列
					for (let j = 0; j <= i; j++) {
						if (j % 2 == 0) {
							if (j == 0) {
								knife.y = 107.5;
							}
							else if (j == 2) {
								knife.y = 97.5;
							}
							else {
								knife.y = -(16.25 * j - 130);
							}
						}
						else {
							if (j == 1) {
								knife.y = 97.5;
							}
							else {
								knife.y = -(16.25 * (j + 1) - 130);
							}
						}
					}
				}
				//右下左上
				// if (this.jiao < 157.5 && this.jiao > 112.5) {
				// 	//knife.x = 75 - 45 * i;
				// 	//knife.y = -75 + 45 * i;
				// }
				// if (this.jiao < -22.5 && this.jiao > -67.5) {
				// 	// knife.x = 75 - 45 * i;
				// 	// knife.y = -75 + 45 * i;
				// }
				// //右上左下
				// if (this.jiao < 67.5 && this.jiao > 22.5) {
				// 	// knife.x = -75 + 45 * i;
				// 	// knife.y = -75 + 45 * i;
				// }
				// if (this.jiao < 247.5 && this.jiao > 202.5) {
				// 	// knife.x = -75 + 45 * i;
				// 	// knife.y = -75 + 45 * i;
				// }
				knife.state = KNIFESTATE.BULLET;
			}
		}
	}
	public scale() {
		if (this.vo.knife_num == 6) {
			this.bg.scaleX = this.bg.scaleY = 1.1;
		}
		if (this.vo.knife_num == 7) {
			this.bg.scaleX = this.bg.scaleY = 1.2;
		}
		if (this.vo.knife_num == 8) {
			this.bg.scaleX = this.bg.scaleY = 1.3;
		}
		if (this.vo.knife_num == 9) {
			this.bg.scaleX = this.bg.scaleY = 1.4;
		}
		return;
	}
	protected updateBg() {
		this.bg_diameter = this.vo.circleRadius * 2;
		this.bg.width = this.bg_diameter;
		this.bg.height = this.bg_diameter;
		this.bg.anchorOffsetX = this.vo.circleRadius;
		this.bg.anchorOffsetY = this.vo.circleRadius;
	}

	protected updateLblName() {
		this.lbl_name.size = this.vo.circleRadius < 50 ? 24 : 32;
		this.lbl_name.width = this.bg_diameter;
		this.lbl_name.anchorOffsetX = this.lbl_name.width / 2;
		this.lbl_name.anchorOffsetY = this.lbl_name.height / 2;
	}
	public updateRadius() {

	}

	public clear() {

	}

	public reset() {

	}

	public destroy() {

	}
}

window['Player'] = Player;